﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class EvecFlighterEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string id = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(id))
                {
                    ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                    ReqEvectionFlighter reqEvectionFlighter = ctx.ReqEvectionFlighters.Single(r => r.Id == int.Parse(id));
                    FlighterName.Value = reqEvectionFlighter.FlighterName;
                    FlighterId.Value = reqEvectionFlighter.FlighterId;
                    FlighterCell.Value = reqEvectionFlighter.FlighterCell;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            ReqEvectionFlighter reqEvectionFlighter;
            string id = Request.QueryString["id"];
            if (string.IsNullOrEmpty(id))
            {
                reqEvectionFlighter = new ReqEvectionFlighter();
                reqEvectionFlighter.FormNo = Request.QueryString["formNo"];
                ctx.ReqEvectionFlighters.InsertOnSubmit(reqEvectionFlighter);
            }
            else
            {
                reqEvectionFlighter = ctx.ReqEvectionFlighters.Single(r => r.Id == int.Parse(id));
            }
            reqEvectionFlighter.FlighterName = FlighterName.Value;
            reqEvectionFlighter.FlighterId = FlighterId.Value;
            reqEvectionFlighter.FlighterCell = FlighterCell.Value;
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='Succeed';window.close();</script>");
        }
    }
}